package com.deli.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deli.springboot.entity.Book;
import com.deli.springboot.entity.ResultDo;
import com.deli.springboot.entity.ResultList;
import com.deli.springboot.service.BookServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 书籍查询和新增
 */


@RestController
public class BookController {

    private static final Logger logger = LoggerFactory.getLogger(BookController.class);
    @Autowired
    private BookServiceImpl bookService;


    /**
     * 通过书名、状态查询图书
     *
     * @param book
     * @return
     */
    @RequestMapping(value = "book/find")
    public String findByExample(Book book) {
        ResultList<Book> resultList = new ResultList();
        if (null == book) {
            resultList.setSuccess(false);
            resultList.setErrorDescription("输入的查询参数book：" + book + " 有误");
            return JSON.toJSONString(resultList);
        }
        List<Book> books = bookService.findByExample(book);
        if (null != books && books.size() > 0) {
            resultList.setSuccess(true);
            resultList.setDataList(books);
            return JSON.toJSONString(resultList);
        }
        resultList.setSuccess(false);
        resultList.setErrorDescription("搜索不到结果");
        return JSON.toJSONString(resultList);
    }

    /**
     * 新增书籍（员工捐赠/内部购买）
     *
     * @param book
     * @return
     */
    @RequestMapping(value = "book/add")
    public String add(Book book) {
        logger.info("go to BookController addMethod book param is " + book);
        ResultDo<Integer> result = new ResultDo<>();
        if (book.getcType() == 0 || book.getcType() == 1) {
            List<Book> books = bookService.findByExample(book);
            if (books != null && books.size() > 0) {
                result.setErrorDescription("该书存在，请重新输入");
                return JSON.toJSONString(result);
            }

            if (null != book && "" != JSON.toJSON(book)) {
                int add = bookService.add(book);
                if (add <= 0) {
                    result.setSuccess(false);
                    result.setErrorDescription("添加新书失败");
                    return JSON.toJSONString(result);
                } else {
                    result.setSuccess(true);
                    return JSON.toJSONString(result);
                }
            }
        }
        result.setSuccess(false);
        result.setErrorDescription("书籍的添加方式有误");
        return JSON.toJSONString(result);
    }

    /**
     * 购书明细列表
     *
     * @param bName
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "purchase/list")
    public String purchaseList(@RequestParam(value = "b_name") String bName,
                               @RequestParam(value = "start_date") Date startDate,
                               @RequestParam(value = "end_date") Date endDate) {
        logger.info("go to BookController purchaseList book param is " + bName);
        long startDateTime = startDate.getTime();
        long endDateTime = endDate.getTime();
        ResultList<Book> result = new ResultList<>();

        Book book = new Book();
        book.setbName(bName);
        List<Book> books = bookService.findByExample(book);
        ArrayList<Book> data = new ArrayList<>();
        if (books != null && books.size() > 0) {
            for (Book bookPojo : books) {
                long cDateTime = bookPojo.getcDate().getTime();
                if (cDateTime >= startDateTime && cDateTime <= endDateTime) {
                    data.add(bookPojo);
                }
            }
            result.setDataList(data);
            result.setSuccess(true);
            return JSON.toJSONString(result);
        }
        result.setSuccess(false);
        result.setErrorDescription("没有查询到记录");
        return JSON.toJSONString(result);
    }

    /**
     * 购书明细表导出
     *
     * @return
     */
   /* @RequestMapping(value = "purchase/export")
    public void purchaseExport(@RequestParam(value = "b_name") String bName,
                               @RequestParam(value = "start_date") Date startDate,
                               @RequestParam(value = "end_date") Date endDate,
                               HttpServletRequest request,HttpServletResponse response) throws IOException {
        String dataString = this.purchaseList(bName, startDate, endDate);
        ResultList resultList = JSON.parseObject(dataString, ResultList.class);
        List<Book> dataList = resultList.getDataList();
        System.out.println("查到的结果是："+dataList);
        ReportExcel reportExcel = new ReportExcel();
        reportExcel.excelExport(dataList,"购书明细列表"+new Date().toString(),Book.class,1,response,request);
    }*/
}

